package com.gxc.service;

import com.alibaba.fastjson.JSON;
import com.gxc.utils.RestControllerResult;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * 发送邮件.
 *
 * @author 宫新程
 * @since 2018/12/5 10:55 AM
 */
@Service
public class SendMailService {

  private Logger logger = LoggerFactory.getLogger(getClass());

  @Resource JavaMailSender mailSender;

  /** 发送普通邮件. */
  public RestControllerResult<Boolean> sendSimpleMail() {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("gongxincheng1@163.com");
    message.setTo("757853223@qq.com");
    message.setSubject("验证码");
    message.setText("验证码为：7578");

    RestControllerResult<Boolean> result = new RestControllerResult<>();
    try {
      String mailContent = JSON.toJSONString(message);
      logger.info("开始发送邮件：{}", mailContent);
      mailSender.send(message);
      logger.info("邮件发送成功");
      result.setSuccess(true);
      return result;
    } catch (Exception e) {
      logger.error("邮件发送失败：{}", e);
      result.setSuccess(false);
      result.setErrorMsg("邮件发送失败" + e);
      return result;
    }
  }
}
